package com.hzcx.app.simplechat.ui.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.common.Common;
import com.hzcx.app.simplechat.ui.publicui.PublicConfirmDialog;
import com.hzcx.app.simplechat.ui.setting.adapter.GroupMessageListAdapter;
import com.hzcx.app.simplechat.ui.setting.bean.GroupMessageListBean;
import com.hzcx.app.simplechat.ui.setting.contract.GroupMessageListContract;
import com.hzcx.app.simplechat.ui.setting.event.GroupMessageEvent;
import com.hzcx.app.simplechat.ui.setting.presenter.GroupMessageListPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 群发助手界面
 */
public class GroupMessageListActivity extends BaseActivity<GroupMessageListPresenter> implements GroupMessageListContract.View, OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_create)
    TextView tvCreate;
    @BindView(R.id.rv_group_msg)
    RecyclerView rvGroupMsg;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;

    private List<GroupMessageListBean> messageData;
    private GroupMessageListAdapter messageListAdapter;
    private int page = 1;
    private boolean isRefresh = true;

    @Override
    public int getLayout() {
        return R.layout.activity_group_message_list;
    }

    @Override
    public void initPresenter() {
        mPresenter = new GroupMessageListPresenter();
    }

    @Override
    public void initView() {

        tvTitle.setText("群发助手");
        ivTitleRight.setImageResource(R.mipmap.ic_icon_group_message_clear);

        messageData = new ArrayList<>();
        messageListAdapter = new GroupMessageListAdapter(messageData);
        rvGroupMsg.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvGroupMsg.setAdapter(messageListAdapter);

        smartRefresh.setOnRefreshListener(this);
        messageListAdapter.setOnLoadMoreListener(this);
    }

    @Override
    public void initData() {
        smartRefresh.autoRefresh();

        messageListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()){
                case R.id.tv_resend: // 再发一次
                    startActivity(new Intent(GroupMessageListActivity.this,GroupMessageSendActivity.class)
                    .putExtra(GroupMessageSendActivity.INTENT_RESEND,messageData.get(position)));
                    break;
            }
        });
    }

    @OnClick({R.id.tv_create, R.id.iv_title_right})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_create:
                startActivity(new Intent(this, GroupMsgFriendListActivity.class));
                break;

            case R.id.iv_title_right:
                showConfirmDialog("清空群发记录", "确认要清空群发记录吗", null, () ->
                        mPresenter.clearGroupMessage(GroupMessageListActivity.this));
                break;
        }
    }

    /**
     * 群发助手列表获取成功
     *
     * @param data
     */
    @Override
    public void groupMessageResult(List<GroupMessageListBean> data) {
        smartRefresh.finishRefresh();
        messageListAdapter.loadMoreComplete();
        try {
            if (isRefresh) {
                messageData.clear();
            }
            messageData.addAll(data);
            messageListAdapter.notifyDataSetChanged();
            if (data.size() < Common.PAGE_COUNT) {
                messageListAdapter.loadMoreEnd();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFail() {
        smartRefresh.finishRefresh();
        messageListAdapter.loadMoreFail();
    }

    /**
     * 清空记录成功
     */
    @Override
    public void clearSuccess() {
        messageData.clear();
        messageListAdapter.notifyDataSetChanged();
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMoreRequested() {
        page++;
        isRefresh = false;
        mPresenter.getGroupMessageList(this, page);
    }

    /**
     * 刷新
     *
     * @param refreshLayout
     */
    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        isRefresh = true;
        mPresenter.getGroupMessageList(this, page);
    }

    /**
     * 刷新列表
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshMessage(GroupMessageEvent event) {
        smartRefresh.autoRefresh();
    }
}
